package com.wlm.controller;

import com.wlm.service.FileService;
import com.wlm.vo.ImageVO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

@RestController
public class FileController {

    @Resource
    private FileService fileService;

    @RequestMapping("/file")
    public String file(MultipartFile fileImage) throws IllegalStateException, IOException {

        // 1. 获取图片的名称
        String fileName = fileImage.getOriginalFilename();

        // 2. 创建文件对象，指定上传目录
        File dir = new File("E:/BaiduNetdiskDownload/jq-images");
        if (!dir.exists()) {
            dir.mkdirs();
        }

        String path = "E:/BaiduNetdiskDownload/jq-images/" + fileName;
        File file = new File(path);

        fileImage.transferTo(file);

        return "文件上传成功!!!";
    }

    @RequestMapping("/pic/upload")
    public ImageVO fileUpload(MultipartFile uploadFile) {
        return fileService.upload(uploadFile);
    }
}
